/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Connections;

import java.sql.CallableStatement;
import java.sql.Connection;
import Object.User;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author nguyen
 */
public class LoginDB {
    private String url;
    private String user;
    private String password;

    public LoginDB(String url, String user, String password) {
        this.url = url;
        this.user = user;
        this.password = password;
    }
    
    public User login(String username, String pwd){
        try {
            Connection conn = ConnectionUtil.getConnection(url, user, password);
            CallableStatement cstmt = conn.prepareCall("{call login (?, ?)}");
            
            cstmt.setString("username", username);
            cstmt.setString("password", pwd);
            
                ResultSet rs = cstmt.executeQuery();
                if (rs.next()) {
                    int userid = rs.getInt("userid");
                    User u = new User(userid, username, pwd);
                    return u;
                }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }  
        
    
}
